Skip to main content

Document Approval.js

DisplayOutstandingAgreements

Display a notification for each agreement that is still pending, including the email address of the contact who has the associated action.

Dependencies

  • Document Approval
    • Agreement Items
    • Outstanding Agreements
DisplayOutstandingAgreements(formContext)
function DisplayOutstandingAgreements(formContext) {
// Retrieve the raw JSON string from the 'tt_agreementitems' and 'tt_outstandingagreements' fields
const agreementItemsText = formContext.getAttribute("tt_agreementitems").getValue();
const outstandingAgreementsText = formContext.getAttribute("tt_outstandingagreements").getValue();

let agreementItems = [];
let outstandingAgreements = [];

// Attempt to parse the JSON strings into JavaScript arrays
try {
agreementItems = JSON.parse(agreementItemsText);
outstandingAgreements = JSON.parse(outstandingAgreementsText);
} catch (error) {
// If parsing fails, log the error and show a form notification
console.error("Error parsing JSON:", error);
formContext.ui.setFormNotification(
"Error parsing agreement data. Please check the format.",
"ERROR",
"json_parse_error"
);
return; // Stop execution if parsing fails
}

// Proceed only if agreementItems is a valid array
if (Array.isArray(agreementItems) && Array.isArray(outstandingAgreements)) {
agreementItems.forEach(item => {
// Find the matching entry in outstandingAgreements by AgreementID
const match = outstandingAgreements.find(oa => oa.AgreementID === item.AgreementID);
// If a match is found and there are outstanding signers
if (match && Array.isArray(match.OutstandingSigners) && match.OutstandingSigners.length > 0) {
const signers = match.OutstandingSigners.map(signer => `${signer}`).join(' and ');

// Construct the message to display
const message = `${item.DocumentName} is awaiting a signature from ${signers}.`;

// Display the message as an informational form notification
formContext.ui.setFormNotification(message, 'INFO', item.AgreementID);
}
});
}
}